package com.chinasoft.ssm.controller;


import com.chinasoft.ssm.pojo.Orderhis;
import com.chinasoft.ssm.pojo.Result;
import com.chinasoft.ssm.service.OrderhisService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@Slf4j
public class OrderhisController {
    @Autowired
    OrderhisService bikeService;


//    @DeleteMapping("/bikedel/{id}")
//    public Result delete(@PathVariable Integer id)
//    {
//        Result result = new Result();
//        Integer data = bikeService.delete(id);
//        result.setData(data);
//        result.setMessage("删除成功");
//        result.setStatus(200);
//        return result;
//
//    }
//    @GetMapping("/bike/selectPage")
//    Result selectPage(Integer current, Integer size) {
//        System.out.println(current + " " + size);
//        // 计算从第几条记录开始
////        Integer start = (current - 1) * size;
//        PageHelper.startPage(current, size);
//
//        Result result = new Result();
//        List<Bike> brands = mapper.findAll();
//        // Page 类 是 List的子类
//        Page<Bike> info = (Page<Bike>) brands;
//        result.setData(info);
//        result.setMessage("分页成功");
//        result.setStatus(200);
//        return result;
//    }

    @GetMapping("/ordersell/{userId}")
    public Result cat(@PathVariable Integer userId) {
        Result result = new Result();
        List<Orderhis> data = bikeService.select(userId);
        result.setData(data);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/ordersel/{id}/{userid}")
    public Result cat1(@PathVariable Integer id,@PathVariable Integer userid) {
        Result result = new Result();
        Orderhis data = bikeService.selectbyId(id,userid);
        if(data!=null){
        result.setData(data);
        result.setMessage("查询成功");
        result.setStatus(200);}
        else
        {
            result.setData(1);
            result.setMessage("查询shibai");
            result.setStatus(400);
            log.info(String.valueOf(result.getStatus()));
        }
        return result;
    }
    @PostMapping("/orderins")
    public Result insert(@RequestBody Orderhis bike) {
        if((bike.getImg())instanceof String)
        {
            log.info("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        }else
        {
            log.info("xx");
        }
        Result result = new Result();
        Integer data = bikeService.insert(bike);
        result.setData(data);
        result.setMessage("插入成功");
        log.info("111");
        result.setStatus(200);
        return result;
    }

    @PostMapping("/orderupd")
        public Result update(@RequestBody Orderhis bike)
        {
            Result result = new Result();
            Integer data = bikeService.update(bike);
            log.info("修改成功");
            result.setData(data);
            result.setMessage("修改成功");
            result.setStatus(200);
            return result;

        }
    @GetMapping("/Aordersel/{id}")
    public Result Aone(@PathVariable Integer id)
    {
        Result result = new Result();
        Orderhis orderhis = bikeService.selectById(id);
        result.setData(orderhis);
        result.setMessage("查询成功");
        result.setStatus(200);
        return result;
    }
    @DeleteMapping("/Aorderdel/{id}")
    public Result Adelete(@PathVariable Integer id)
    {
        Result result = new Result();
        Integer data = bikeService.deleteById(id);
        result.setData(data);
        result.setMessage("删除成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/deleteMoreOrder")
    Result deleteMoreOrder(@RequestBody int[] ids) {

        Result result = new Result();
        log.info(ids.toString() + "ids");
        int data=0;
        for(int i:ids){
            data+=bikeService.deleteById(i);
        }
        result.setData(data);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/Aorder/selectPage/{current}/{size}")
    Result selectPage(@PathVariable Integer current,@PathVariable Integer size) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        Result result = new Result();
        List<Orderhis> orderhis1 = bikeService.selectAll();
        int total = orderhis1.size();
        result.setTotal(total);
        PageHelper.startPage(current, size);

        List<Orderhis> orderhis = bikeService.selectAll();

        // Page 类 是 List的子类
        Page<Orderhis> info = (Page<Orderhis>) orderhis;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }

    @PostMapping("/AorderSelDsql/{current}/{size}")
    Result selectPage(@PathVariable Integer current, @PathVariable Integer size,@RequestBody Orderhis orderhis) {
        System.out.println(current + " " + size);
        // 计算从第几条记录开始
//        Integer start = (current - 1) * size;
        PageHelper.startPage(current, size);

        Result result = new Result();
        List<Orderhis> orderhis1 = bikeService.selectByDSql(orderhis);
        int total = orderhis1.size();
        result.setTotal(total);
        // Page 类 是 List的子类
        Page<Orderhis> info = (Page<Orderhis>) orderhis1;
        result.setData(info);
        result.setMessage("分页成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/Aorderins")
    public Result Ainsert(@RequestBody Orderhis orderhis) {
        log.info(orderhis.toString());
        Result result = new Result();
        Integer data = bikeService.add(orderhis);
        result.setData(data);
        result.setMessage("插入成功");
        result.setStatus(200);
        return result;
    }
    @PostMapping("/Aorderupd")
    public Result Aupdate(@RequestBody Orderhis orderhis)
    {
        Result result = new Result();
        Integer data = bikeService.update(orderhis);
        result.setData(data);
        result.setMessage("修改成功");
        result.setStatus(200);
        return result;
    }
    @GetMapping("/stats")
    public ResponseEntity<?> getStatistics() {
        Integer totalOrders = bikeService.countTotalOrders();
        Double totalCosts = bikeService.sumTotalCosts();
        // 聚合其他统计数据

        Map<String, Object> stats = new HashMap<>();
        stats.put("totalOrders", totalOrders);
        stats.put("totalCosts", totalCosts);
        // 添加其他统计数据

        return ResponseEntity.ok(stats);
    }
}
